package xmltext;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class Bossxml {

	public static void main(String[] args) {
		String fileName = "D:\\eclipse\\code1\\src\\info.txt";
		Bosslist bosslist = new Bosslist();
		List<Boss>regions = Bossparse(fileName);
		bosslist.setBoss(regions);
		JAXBContext ctx = null;
		try {
			ctx = JAXBContext.newInstance(Bosslist.class);
			ctx.createMarshaller().marshal(bosslist, new File("C:\\Users\\qingke01\\Desktop\\Boss.xml"));
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static List<Boss> Bossparse(String fileName){
		
		File file = new File(fileName);
		List<Boss> region = new ArrayList<Boss>();
		BufferedReader breader = null;
			try {
				FileReader freader = new FileReader(file);
				breader = new BufferedReader(freader);
				String line = null;
				while((line = breader.readLine())!= null){
					line = line.trim();
					if(line.startsWith("#")){
						continue;
					}
					String[] strings = line.split(",");
					String id,name,averincome,childid,boss;
					Boss boss2 = new Boss();
					id = strings[0];
					name = strings[1];
					averincome = strings[2];
					childid = strings[3];
					boss = (strings.length==4) ? "":strings[4];
					boss2.setId(id) ;
					boss2.setName(name);
					boss2.setAverincome(averincome);
					boss2.setChildid(averincome);
					boss2.setBoss(boss);
					region.add(boss2);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(breader!=null){
					try {
						breader.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			return region;
		}
}